package spring.cloud.tasks.executor.executor.task.execute_statistics;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.executor.executor.Executor;
import spring.cloud.tasks.executor.executor.task.TaskExecutor;
import spring.cloud.tasks.executor.executor.task.task_trigger.TaskExecutorInfoService;

import java.util.ArrayList;
import java.util.List;

/**
 * 统计处理数据数量的任务.
 */
@Slf4j
@Setter
@Getter
public class ExecuteStatisticsReportTask implements Runnable {

    private String executorId;
    private Executor executor;

    public ExecuteStatisticsReportTask(String executorId) {
        this.executorId = executorId;
        this.executor = null;
    }

    @Override
    public void run() {
        List<String> taskIdList = new ArrayList<>(this.executor.taskIdToTaskExecutorMap.taskIdSet);
        for (String taskId : taskIdList) {
            TaskExecutor taskExecutor = this.executor.taskIdToTaskExecutorMap.getTaskExecutor(taskId);
            if (taskExecutor != null) {
                TaskExecutorInfoService taskExecutorInfoService = taskExecutor.getTaskExecutorInfoService();
                taskExecutorInfoService.persistProcessSuccessCount(ExecuteStatistics.getProcessSuccessCount(taskId));
                taskExecutorInfoService.persistProcessFailureCount(ExecuteStatistics.getProcessFailureCount(taskId));
            }

        }


    }
}
